<?php

require '../php-sdk/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
function createAppInstance(){

	return $facebook = new Facebook(array(
		'appId'  => '176213615761250',
		'secret' => '375110a154f2a4f5b2f6e4f8f093f7be',
	));
}
	
function getUsername() {

	$facebook = createAppInstance();
	$userID = $facebook->getUser();
	
	if($userID) {

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {

        $user_profile = $facebook->api('/me','GET');
        return $user_profile['name'];

      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl(); 
        echo 'Please &lt;a href=&quot;' . $login_url . '&quot;&gt;login.&lt;/a&gt;';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {

      // No user, print a link for the user to login
      $login_url = $facebook->getLoginUrl();
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }
}
	
?>
